package fxification.db;

import com.sun.javafx.runtime.TypeInfo;
import com.sun.javafx.runtime.sequence.ObjectArraySequence;
import com.sun.javafx.runtime.sequence.Sequence;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Iterator;

public class DataSet {
    private int columnCount;
    private ObjectArraySequence<DataSet.Row> rows = new ObjectArraySequence(
        TypeInfo.makeTypeInfo(DataSet.Row.class),
        new ArrayList<DataSet.Row>()
    );

    private ResultSet resultSet;
    private ResultSetMetaData metaData;


    public int getColumnCount() {
        return columnCount;
    }

    public void setColumnCount(int columnCount) {
        this.columnCount = columnCount;
    }

    public ResultSet getResultSet() {
        return resultSet;
    }

    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    public Sequence<DataSet.Row> getRows() {
        return rows;
    }

    public void setRows(Sequence<DataSet.Row> rows) {
        this.rows = (ObjectArraySequence<DataSet.Row>) rows;
    }

    public ResultSetMetaData getMetaData() {
        return metaData;
    }

    public void setMetaData(ResultSetMetaData metaData) {
        this.metaData = metaData;
    }

    public DataSet addRow(DataSet.Row row){
        rows.add(row);
        return this;
    }

    public Row get(int i){
        return rows.get(i);
    }

    public static class Column {
        private String catalogName;
        private String tableName;
        private String name;
        private String type;
        private int size;
        private int scale;
        private String schema;
        private boolean autoIncrement;
        private Object value;

        public boolean isAutoIncrement() {
            return autoIncrement;
        }

        public void setAutoIncrement(boolean autoIncrement) {
            this.autoIncrement = autoIncrement;
        }

        public String getCatalogName() {
            return catalogName;
        }

        public void setCatalogName(String catalogName) {
            this.catalogName = catalogName;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getScale() {
            return scale;
        }

        public void setScale(int scale) {
            this.scale = scale;
        }

        public String getSchema() {
            return schema;
        }

        public void setSchema(String schema) {
            this.schema = schema;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public String getTableName() {
            return tableName;
        }

        public void setTableName(String tableName) {
            this.tableName = tableName;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }

    public static class Row{
        private ObjectArraySequence<DataSet.Column> columns =
            new ObjectArraySequence(
                TypeInfo.makeTypeInfo(DataSet.Column.class),
                new ArrayList<DataSet.Column>()
             );

        public Sequence<DataSet.Column> getColumns() {
            return columns;
        }

        public Row add(DataSet.Column col){
            columns.add(col);
            return this;
        }

        public DataSet.Column getColumn(int pos){
            return columns.get(pos);
        }

        public DataSet.Column getColumn(String colName){
            DataSet.Column column = null;
            for(Iterator<DataSet.Column> col = columns.iterator(); col.hasNext();){
                column = col.next();
                if(column.getName().equals(colName)){
                    break;
                }
            }
            return column;
        }
    }
}
